package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.jspsmart.upload.Request;
import com.ysd.entity.XinWen;
import com.ysd.util.DBManager;

public class XinWenDaoimpl {
	/**
	 * 获得新闻分类的数据
	 * @return
	 */
	public List<XinWen> getXinWen() {
		
		Connection conn=DBManager.getConnection();
		List<XinWen> list =new ArrayList<XinWen>();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			 st=conn.createStatement();
			String sql="SELECT * FROM xinWen ";
			 rs=st.executeQuery(sql);
			while(rs.next()) {
				int xinWenId=rs.getInt("xinWenId");
				String xinWenName=rs.getString("xinWenNameFenLei");
				 XinWen xw=new XinWen(xinWenId, xinWenName);
				 list.add(xw);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	
	public List<XinWen> getXinWenByPage(int page,int size) {
		
		Connection conn=DBManager.getConnection();
		List<XinWen> list =new ArrayList<XinWen>();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			 st=conn.createStatement();
			String sql="SELECT * FROM xinwen LIMIT "+(page-1)*size+","+size;
			 rs=st.executeQuery(sql);
			while(rs.next()) {
				int xinWenId=rs.getInt("xinWenId");
				String xinWenName=rs.getString("xinWenNameFenLei");
				 XinWen xw=new XinWen(xinWenId, xinWenName);
				 list.add(xw);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 根据id删除对应的数据
	 * @param id
	 * @return
	 */
	public int delXinWenFenLei(int id) {
		return DBManager.executeUpdate("delete from xinwen WHERE xinWenId="+id);
	}
	
	
	/**
	 * 添加一条数据
	 * @param x
	 * @return
	 */
	public int addXinWenFenLei(XinWen x) {
		return DBManager.executeUpdate("INSERT INTO xinwen VALUES(null,'"+x.getXinWenNameFenLei()+"')");
	}
	
	
	public ArrayList<XinWen> selXinWenFenLeiByName(String name){
		Connection conn=DBManager.getConnection();
		 ArrayList<XinWen>  list=new ArrayList<XinWen>();
		Statement st=null;
		ResultSet rs=null;
		try {
			 st=conn.createStatement();
			 rs=st.executeQuery("SELECT * from xinwen where xinWenNameFenLei='"+name+"'");
			while(rs.next()) {
				int xinWenId=rs.getInt("xinWenId");
				String xinWenNameFenLei=rs.getString("xinWenNameFenLei");
				XinWen xw=new XinWen(xinWenId, xinWenNameFenLei);
				list.add(xw);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	
	
	/**
	 * 根据新闻分类id查询新闻分类
	 * @param id
	 * @return
	 */
	public XinWen getXinwenFenLeiById(int id) {
		Connection conn=DBManager.getConnection();
		XinWen xw=null;
		Statement st=null;
		ResultSet rs=null;
		try {
			 st=conn.createStatement();
			 rs=st.executeQuery("SELECT * FROM xinwen WHERE xinWenId="+id);
			while(rs.next()) {
				 xw=new XinWen(rs.getInt("xinWenId"), rs.getString("xinWenNameFenLei"));
			}
			return xw;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	public int editXinWenFenLei(XinWen xw) {
		return DBManager.executeUpdate("UPDATE xinwen set  xinwennamefenlei='"+xw.getXinWenNameFenLei()+"' where xinwenid="+xw.getXinWenId());
	}
	
	
	public ArrayList<XinWen> getXinwenFenLeiSize() {
		ArrayList<XinWen> list=new ArrayList<XinWen>();
		Connection conn=DBManager.getConnection();
		XinWen xw=null;
		Statement st=null;
		ResultSet rs=null;
		try {
			 st=conn.createStatement();
			 rs=st.executeQuery("SELECT xinWenId FROM xinwen");
			while(rs.next()) {
				 xw=new XinWen(rs.getInt("xinWenId"));
				list.add(xw) ;
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
}
